<?php
class Admin_studyAction extends Action{
	function lists(){
		AdminAction::check();

		$user = D('study');
		$count = $user->count();
		$pre_page = 10;
		$p=intval($_GET['p']);
		$p=$p==0 ? 1 : $p;
		$start=($p-1)*$pre_page;
		$list = $user->limit("$start,$pre_page")->findAll();
        
		import('ORG.Util.Page');
		$page = new Page($count,$pre_page);
		$pages = $page->show();
		$this->assign('pages',$pages);
		
		$this->assign('list',$list);
		$this->display('admin_study@lists');
	}
	
	function post(){
		AdminAction::check();
		
		$get_id = intval($_GET['id']);
		$post_id = intval($_POST['id']);
		$hid = $_POST['hid'];
		
		if($hid==1){
			$arr['sid'] = intval($_POST['sid']);
			$arr['title'] = addslashes($_POST['title']);
			$arr['keywords'] = addslashes($_POST['keywords']);
			$arr['description'] = addslashes($_POST['description']);
			$content = addslashes(($_POST['content']));
			$arr['content'] = str_replace('\\','',$content);
			
			$user = D('study');
			if($post_id>0){
				$re = $user->where('id='.$post_id)->save($arr);
			}else {
				$arr['send_time'] = time();
				$re = $user->add($arr);
			}
			if($re){
				go('/Admin_study/lists','操作成功!');
			}
		}
		if($get_id>0){
			$user = D('study');
			$lab = $user->where('id='.$get_id)->find();
			$this->assign('lab',$lab);
		}
		
		$user = D('study_sort');
		$slist = $user->field('sid,sname')->where('pid=0')->findAll();
		$this->assign('slist',$slist);
		
		$this->display('admin_study@post');
	}
	
	/***********************************************/
	
	function slist(){
		AdminAction::check();
		
		$user = D('study_sort');
		$slist = $user->findAll();
		$this->assign('slist',$slist);
		$this->display('admin_study@slist');
	}
	
	function spost(){
		AdminAction::check();
		$user = D('study_sort');
		
		if($_POST['hid']==1){
			if($_FILES['photo']['size']>0){
				import('ORG.Net.UploadFile');
				$upload = new UploadFile();
				$upload->savePath = './upload/study/';
				
				if($_POST['old_photo']!=''){
					$upload->saveRule = $_POST['old_photo'];
				}else{
					$upload->saveRule = 'time';
				}
				
				$upload->thumbPrefix='php_';
				$upload->thumb = true;
				$upload->thumbMaxHeight=220;
				$upload->thumbMaxWidth = 220;
				$upload->thumbRemoveOrigin = true;
				$upload->uploadReplace= true;
				if($upload->upload()){
					$info = $upload->getUploadFileInfo();
					$arr['pic'] = $info[0]['savename'];
				}
			}
			
			$arr['pid']=intval($_POST['pid']);
			$arr['sname']=addslashes($_POST['sname']);
			$arr['surl']=addslashes($_POST['surl']);
			$arr['about']=addslashes($_POST['about']);
			
			$post_sid = intval($_POST['sid']);
			if($post_sid>0){
				$re = $user->where('sid='.$post_sid)->save($arr);
			}else {
				$re = $user->add($arr);
			}
			
			if($re){
				go('/Admin_study/slist','操作成功!');
			}
		}
		
		$get_sid = intval($_GET['sid']);
		if($get_sid>0){
			$lab=$user->where('sid='.$get_sid)->find();
			$photo_arr = explode('.',$lab['pic']);
			$lab['old_photo'] = $photo_arr[0];
			$this->assign('lab',$lab);
		}
		
		$slist = $user->where('pid=0')->findAll();
		$this->assign('slist',$slist);
		$this->display('admin_study@spost');
	}
}
?>